package com.yxb.gateway;

import com.alibaba.fastjson.JSONObject;
import com.yxb.constant.DataPointValue;
import com.yxb.constant.SendMsgEvent;
import com.yxb.option.DataPoint;
import com.yxb.option.DeviceInfoVo;
import com.yxb.service.common.domain.DeviceData;
import com.yxb.service.common.interf.DeviceDataService;
import com.yxb.util.CHexConver;
import com.yxb.util.RequestUtil;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author
 */
@RestController
@RequestMapping("gate-way")
public class GateWayIndexController {

    private static final Logger LOGGER = LoggerFactory.getLogger(GateWayIndexController.class);

    @Resource
    private DeviceDataService  deviceDataService;



    @RequestMapping(value ="/data/save", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<String> saveData( HttpServletRequest request){
        try{
            String paramStr = RequestUtil.getRequestJsonString(request);
            LOGGER.info("paramStr---->{}", paramStr);
            DeviceInfoVo deviceInfoVo = JSONObject.parseObject(paramStr, DeviceInfoVo.class);
            if ( !deviceInfoVo.getType().equalsIgnoreCase(SendMsgEvent.SYNC) ){
                LOGGER.info("sendMsgEvent type :{} not SYNC", deviceInfoVo.getType());
                return ResponseEntity.ok("error");
            }
            ImmutablePair<Boolean,String> immutablePair  =  deviceDataService.addObject(deviceInfoVo);
            if ( !immutablePair.getLeft() ){
                return ResponseEntity.ok("error");
            }

        } catch (IOException e) {
            LOGGER.error("{}",e);
        }

        return ResponseEntity.ok("");
    }

    public static void main(String[] args) {
        LOGGER.error("error");

    }

}
